<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage;

class ActivityInformation extends Model
{
    use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'activity_information';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'act_id',
        'cover',
        'video',
        'activity_theme',
        'medical_template',
        'entry_process_pc',
        'entry_process_mobile',
        'entry_information',
        'expert_judge',
    ];

    protected $hidden = [
        'created_at',
        'updated_at',
        'deleted_at'
    ];

    // protected $casts = [
    //     'expert_judge' => 'array'
    // ];
    public function getExpertJudgeAttribute($value)
    {
        return json_decode($value,true);
    }
    
    public function getCoverAttribute($value)
    {
        if ($this->or_url($value)) {
            return $value;
        } else {
            return config('filesystems.disks.qiniu.domains.https') . $value;
        }
    }
    
    public function getVideoAttribute($value)
    {
        if ($this->or_url($value)) {
            return $value;
        } else {
            return config('filesystems.disks.qiniu.domains.https') . $value;
        }
    }

    public function getMedicalTemplateAttribute($value)
    {
        if ($this->or_url($value)) {
            return $value;
        } else {
            return config('filesystems.disks.qiniu.domains.https') . $value;
        }
    }

    public function getEntryProcessPcAttribute($value)
    {
        if ($this->or_url($value)) {
            return $value;
        } else {
            return config('filesystems.disks.qiniu.domains.https') . $value;
        }
    }

    public function getEntryProcessMobileAttribute($value)
    {
        if ($this->or_url($value)) {
            return $value;
        } else {
            return config('filesystems.disks.qiniu.domains.https') . $value;
        }
    }



    /**
     * 判断地址是否是url地址
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-04-11
     * @param string $url
     * @return bool
     */
    public function or_url($url)
    {

        if (empty($url)) {
            return true;
        }
        $preg = "/http[s]?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is";

        if (preg_match($preg, $url)) {

            return true;
        } else {

            return false;
        }
    }

    /**
     * 下载病例模板
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2023-02-08
     * @param int $act_id
     * @return array
     */
    public static function geMedicalTemplate($act_id)
    {
        $info = ActivityInformation::where('act_id', $act_id)->first();
        if (empty($info)) {
            return fail_message('未找到');
        }
        return ['template_url' => $info->medical_template];
    }
}
